<template>
  <el-form ref="biologyRef" :model="biologyform" label-width="120px" label-position="right" :rules="biologyrules">
    <el-form-item label="时间步长：" prop="timeStep">
      <el-input v-model="biologyform.timeStep"></el-input>
    </el-form-item>
    <el-form-item label="起始位置：" prop="startLocation">
      <el-input v-model="biologyform.startLocation"></el-input>
    </el-form-item>
    <el-form-item label="初始感染人数：" prop="initialInfectedCount">
      <el-input v-model="biologyform.initialInfectedCount"></el-input>
    </el-form-item>
    <el-form-item label="感染速率：" prop="infectionRate">
      <el-input v-model="biologyform.infectionRate"></el-input>
    </el-form-item>
    <el-form-item label="接触频率：" prop="contactFrequency">
      <el-input v-model="biologyform.contactFrequency"></el-input>
    </el-form-item>
    <el-form-item label="康复速率：" prop="recoveryRate">
      <el-input v-model="biologyform.recoveryRate"></el-input>
    </el-form-item>
    <el-form-item label="转换速率：" prop="conversionRate">
      <el-input v-model="biologyform.conversionRate"></el-input>
    </el-form-item>
    <el-form-item label="每日流动人数：" prop="dailyFlowCount">
      <el-input v-model="biologyform.dailyFlowCount"></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, onMounted, watchEffect } from 'vue'
import { validateNumber } from '@/utils/validate'

const props = defineProps({
 
  formtext: {
    type: String,
    default: ''
  }
})


const emit = defineEmits([
  'formsubmit',
  'cancel',
  'reset',
])

const biologyform = ref({
  timeStep: "",
  startLocation: "",
  initialInfectedCount: null,
  infectionRate: null,
  contactFrequency: null,
  recoveryRate: null,
  conversionRate: null,
  dailyFlowCount: null
})

const biologyRef = ref()

const biologyrules = ref({
  timeStep: [{
    required: true, message: '请输入时间步长', trigger: 'blur'

  }],
  startLocation: [{
    required: true, message: '请输入起始位置', trigger: 'blur'

  }],
  initialInfectedCount: [
    {
      required: true, message: '请输入初始感染人数', trigger: 'blur'
    },
    { validator: validateNumber, trigger: 'blur' }
  ],

  infectionRate: [{

    required: true, message: '请输入感染速率', trigger: 'blur'
  }
    , { validator: validateNumber, trigger: 'blur' }
  ],
  contactFrequency: [{
    required: true, message: '请输入接触频率', trigger: 'blur'

  }
    , { validator: validateNumber, trigger: 'blur' }],
  recoveryRate: [{

    required: true, message: '请输入康复速率', trigger: 'blur'
  }, { validator: validateNumber, trigger: 'blur' }],
  conversionRate: [{
    required: true, message: '请输入转换速率', trigger: 'blur'

  }, { validator: validateNumber, trigger: 'blur' }],
  dailyFlowCount: [{
    required: true, message: '请输入每日流动人数', trigger: 'blur'
  }, { validator: validateNumber, trigger: 'blur' }],

})


const handleSubmit = async (btntype) => {

  if (!biologyRef.value) return
  try {
    await biologyRef.value.validate((valid) => {
      if (valid) {


        biologyform.value.initialInfectedCount = Number(biologyform.value.initialInfectedCount)
        biologyform.value.infectionRate = Number(biologyform.value.infectionRate)
        biologyform.value.contactFrequency = Number(biologyform.value.contactFrequency)
        biologyform.value.recoveryRate = Number(biologyform.value.recoveryRate)
        biologyform.value.conversionRate = Number(biologyform.value.conversionRate)
        biologyform.value.dailyFlowCount = Number(biologyform.value.dailyFlowCount)

        if (btntype == '模板') {
          emit('formsubmit', biologyform.value, { btntype: btntype })
        } else {
          emit('formsubmit', biologyform.value)
        }
      }
    })
  } catch {

  }
}

const handleReset = () => {
  if (biologyRef.value) {
    biologyRef.value.resetFields()
    // 重置表单数据为初始值
    Object.keys(biologyform.value).forEach(key => {

      biologyform.value[key] = ''
    })
  }
}

defineExpose({
  handleSubmit,
  handleReset,
})

onMounted(() => { })


watchEffect(() => {
  let data = sessionStorage.getItem('mnxl_data')
  if (data) {
    data = JSON.parse(data)
    if (data.simulatedformdata.simulateEventType == props.formtext) {
      biologyform.value = data.formdatadata
    }
  }
})

</script>

<style lang="less" scoped></style>